home *** CD-ROM | disk | FTP | other *** search
- /* XOMAIL.C ver .840415 Mail module for W2XO bbs Jim Durham, W2XO, 4-15-89 */
-
- /* Code released to the Amateur Radio community*/
-
- #include "xobbs.h"
- #ifdef hpux
- #define locking lockf
- #endif
-
- char tbuf[130];
-
- sendmsg(mode) /*enter a message in data base*/
- int mode;
- {
- char *p;
- static char ch = '\n';
- int fd,fd2; /*file discriptors*/
- int mprmpt; /*boolean: Print message prompt line*/
- int i,n,finish;
- int z;
- int bidrcvd; /* boolean 'bid received' */
-
- static int thistim=0; /* how many messages sent by this user this time*/
- static char newpath[128]; /* path to mail directory*/
-
- static char headerbuf[128]; /* buffer space for header */
- static struct msghdr mhdr; /* place to assemble message header*/
- char buf[4]; /* small buffer */
-
- bidrcvd=0; /* no bid received yet */
- mhdr.siz = 0; /* set size to 0 */
- thistim++; /* increment this time msg count*/
-
- buf[0]=buf[1]=buf[2]='\0'; /*clear the buffer*/
- strcpy(mhdr.atbbs,""); /*blank the @ bbs field*/
-
- if(strlen(command.fld[1]) < 1) /*no fields, error*/
- {
- printerr();
- return(0);
- }
- if(mode == 1){
- sprintf(prinbuf,"File to make into message?\n");
- prinout(FLUSH);
- getline(1);
- if((fd = open(inline,O_RDONLY,0666)) < 1){
- sprintf(prinbuf,"Can't open %s\n",inline);
- prinout(FLUSH);
- close(fd);
- return;
- }
- }
- strcpy(mhdr.tocall,command.fld[1]);
- if(strlen(command.fld[2]) < 1) /*only "to" field pres.*/
- {
- strcpy(mhdr.fromcall,user.call);
- goto domsg;
- }
- switch(command.fld[2][0])
- {
- case '<': strcpy(mhdr.fromcall,command.fld[3]);
- if(command.fld[4][0]=='$'){
- strcpy(mhdr.bid,&command.fld[4][1]);
- bidrcvd=1;
- }
- goto domsg;
- break;
- case '@': strcpy(mhdr.atbbs,command.fld[3]);
- if(command.fld[4][0]=='$'){
- strcpy(mhdr.bid,&command.fld[4][1]);
- bidrcvd=1;
- goto domsg;
- }
- break;
- case '$': strcpy(mhdr.bid,&command.fld[2][1]); /*copy bid to proper field*/
- bidrcvd=1; /*set flag*/
- strcpy(mhdr.fromcall,user.call);
- goto domsg;
- break;
- }
- if(strlen(command.fld[4]) < 1) /* no more fields */
- {
- strcpy(mhdr.fromcall,user.call);
- goto domsg;
- }
-
- switch(command.fld[4][0]){
- case '<': break; /*field must start with < */
- case '$': strcpy(mhdr.fromcall,user.call);
- goto domsg;
- break;
- default: printerr();
- return 0;
- break;
- }
-
- strcpy(mhdr.fromcall,command.fld[5]); /* get from call */
- if(command.fld[6][0] == '$'){
- strcpy(mhdr.bid,&command.fld[6][1]); /* get the bid */
- bidrcvd=1;
- }
- domsg:
- mprmpt=false;
-
- if((revfwd && bidrcvd) && user.typ=='B'){ /* if a bid was received */
- if(chkbid(mhdr.bid)){ /*check if we already have it*/
- sprintf(prinbuf,"OK - Send It\n");
- prinout(FLUSH);
- }
- else{
- sprintf(prinbuf,"NO - Already Have It\n");
- prinout(NOFLUSH);
- return(1); /*back to the prompt*/
- }
- }
- else if((revfwd && (!bidrcvd)) && user.typ=='B'){
- sprintf(prinbuf,"OK\n");
- prinout(FLUSH);
- }
- else if(user.typ == 'B'){
- mprmpt=true;
- sprintf(prinbuf,"Sj:\n");
- prinout(FLUSH);
- }
- else{
- mprmpt=true;
- sprintf(prinbuf,"Subject?:\n"); /* inquire as to subject */
- prinout(FLUSH);
- }
-
- getline(0); /* and input it */
- strcpy(mhdr.title,inline);
- mhdr.type=command.opt; /* set type and status */
- mhdr.stat='N';
-
- gettim(); /* get the entry time */
- sprintf(prinbuf,"%02d%02d%02d\0",newtime->tm_year,newtime->tm_mon+1,newtime->tm_mday);
- strcpy(mhdr.dat,prinbuf);
- sprintf(prinbuf,"%02d%02d\0",newtime->tm_hour,newtime->tm_min);
- strcpy(mhdr.tim,prinbuf);
- if((mhdr.type=='B' || mhdr.type=='A')&& !bidrcvd) /*if A or B type and no bid*/
- sprintf(mhdr.bid,"%s%d",my.call,pid);
-
- /* END OF MESSAGE HEADER INPUT */
-
- /* make a temp file for message based on process id, and the */
- /* number of messages for this connect for this user */
-
-
-
-
-
- sprintf(newpath,"%st%d.%d\0",tempdir,thistim,pid);
-
- if((fd2=open(lowcase(newpath),O_RDWR|O_CREAT,0x1b6)) < 0)
- {
- perror("bbs:Cant Open new file ");
- sndupkil();
- }
- /*load the header buffer */
- sprintf(headerbuf,
- "%-5d %c%c %-6.6s %-6.6s %-6.6s %-6.6s %-4.4s %-30.30s %-12.12s\n"
- ,mhdr.siz,mhdr.type,mhdr.stat,mhdr.tocall,mhdr.fromcall,mhdr.atbbs,mhdr.dat,mhdr.tim,mhdr.title,mhdr.bid);
- p=headerbuf; /*write out the header*/
- while(*p) write(fd2,p++,1);
-
- if(mode == 0){
- if(mprmpt){
- sprintf(prinbuf,"Send message (/EX or Control-Z to end)\n");
- prinout(FLUSH);
- }
-
- do{
- getline(1); /*get a line*/
- n = strlen(inline); /*compute length*/
- finish = 0; /*not done yet*/
- for(i=0;i < n; i++){ /*control Z check*/
- if(inline[i] == 26){
- inline[i] = '\0';
- n = strlen(inline);
- finish = 1;
- break;
- }
- }
- if(!finish) /* ex check*/
- if(inline[0] == '/')
- if((inline[1] == 'E' || inline[1] == 'e')
- && (inline[2] == 'X' || inline[2] == 'x')){
- n = 0;
- finish = 1;
- }
- write(fd2,inline,n); /*write the line to file*/
- write(fd2,&ch,1); /*and a NL */
- mhdr.siz += n; /* add on to size */
- } while(!finish);
- } /* if mode 0 */
- else{
- while(n = fgetline(fd,inline)){ /*input is from file*/
- if((z=write(fd2,inline,n)) < n){
- sprintf(prinbuf,"Error writing file\n");
- prinout(FLUSH);
- }
- mhdr.siz += n;
- write(fd2,&ch,1); /*and a NL */
- }
- close(fd);
- }
-
- lseek(fd2,0l,0); /*back to beginning of file*/
- sprintf(headerbuf,"%-5d",mhdr.siz);
- write(fd2,headerbuf,5); /*put the size on the file*/
- close(fd2);
- return(1);
- }
-
-
- phdr() /*print a header legend*/
- {
- sprintf(prinbuf,"\nMsg# Size TS To From BBS/NT Date Time Subject\n");
- prinout(NOFLUSH);
- sprintf(prinbuf,"----- ----- -- ------ ------ ------ ------ ---- ------------------------------\n");
- prinout(NOFLUSH);
- }
-
- lsthdrs()
- {
- char s[20];
- char hdr[130];
- struct msghdr mhdr;
- int fd2,start,end,cnt,ok,linecnt,hdrdone;
-
- if((fd2=openhdrfil()) < 0){
- sprintf(prinbuf,"lsthdrs: Can't open header file\n");
- perror(prinbuf);
- return(0);
- }
-
- end=atoi(command.fld[1]);
- cnt=0;
- linecnt=0;
- hdrdone = 0;
-
- while(read(fd2,hdr,92)){ /*main loop*/
- conhdr(hdr+6,&mhdr); /*get header*/
- sprintf(s,"%5.5s",hdr); /*get file name*/
-
- switch(command.opt){
- case ' ': break;
-
- case 'l':
- case 'L': if(cnt < end)
- break;
- else
- goto dun;
- break;
- case 'y':
- case 'Y': if(mhdr.stat == 'Y')
- break;
- else
- continue;
- break;
- case 'n':
- case 'N': if(mhdr.stat == 'N')
- break;
- else
- continue;
- break;
- case '@': if(matchn2(command.fld[1],mhdr.atbbs,strlen(mhdr.atbbs)))
- break;
- else
- continue;
- break;
- case '<': if(matchn2(command.fld[1],mhdr.fromcall,strlen(mhdr.fromcall)))
- break;
- else
- continue;
- break;
- case '>': if(matchn2(command.fld[1],mhdr.tocall,strlen(mhdr.tocall)))
- break;
- else
- continue;
- break;
- case 'M':
- case 'm': if(matchn2(user.call,mhdr.tocall,strlen(mhdr.tocall))
- || matchn2(user.call,mhdr.fromcall,strlen(mhdr.fromcall)))
- break;
- else
- continue;
- break;
- case '?': if((indexn(upcase(mhdr.title),
- upcase(command.fld[1]),20)) >= 0)
- break;
- else
- continue;
- break;
- default: if(mhdr.type == command.opt)
- break;
- else
- continue;
- break;
- }/*end of switch*/
-
- /*if only the first field*/
- if(command.fld[2][0] == '\0' && command.fld[3][0] == '\0'){
- start=atoi(command.fld[1]);
- if(atoi(s) >= start)
- goto nxt;
- else
- continue;
- }
- /* if 1st and second fields*/
- if(command.fld[2][0] != '\0' && command.fld[3][0] == '\0'){
- start=atoi(command.fld[1]);
- end=atoi(command.fld[2]);
- if(atoi(s) >= start && atoi(s) <= end)
- ;
- else
- continue;
- }
-
- nxt: if(mhdr.type == 'P' && user.typ != 'S'){
- if( (strcmp(mhdr.tocall,user.call))
- && (strcmp(mhdr.fromcall,user.call)) )
- continue;
- }
-
- if(mhdr.stat == 'X') /*don't list killed messages*/
- continue;
-
- if(!hdrdone){
- phdr();
- hdrdone = 1;
- }
- sprintf(prinbuf,"%75.75s\n",hdr);
- prinout(NOFLUSH);
- cnt++;
- linecnt++;
- if(linecnt > 15){
- linecnt=0;
- sprintf(prinbuf,"Continue Listing? (Yes or No)\n");
- prinout(FLUSH);
- getline(0);
- if(inline[0] != 'Y' && inline[0] != 'y')
- break;
- }
- } /*end of while fgetline*/
- dun: ;
- if(!cnt)
- printnone();
- close(fd2);
- return(1);
- }
-
- readmail() /*read up to 9 messages*/
- {
- static char cmdstr[30];
- int i,fd,fd2,msgnum;
- int fptr;
- char c;
- struct msghdr mhdr;
- char hdr[128], *cp;
- int bufcnt,linectr;
- char temp[3];
-
- i =1;
- fptr = 0;
-
- while(command.fld[i][0] != '\0' && i < 9){ /*do as many msgs as there*/
- /* are arguments < 9 */
- msgnum = atoi(command.fld[i]);
- sprintf(cmdstr,"%s%d",maildir,msgnum); /*get file path*/
- if((fd=open(cmdstr,O_RDWR)) <1){
- sprintf(prinbuf,"No message %s\n",command.fld[i]);
- prinout(NOFLUSH);
- i++;
- continue;
- }
- read(fd,hdr,86);
-
- conhdr(hdr,&mhdr);
- if(mhdr.type == 'P' && user.typ != 'S'){
- if( (strcmp(mhdr.tocall,user.call))
- && (strcmp(mhdr.fromcall,user.call)) ){
- sprintf(prinbuf,"Message not to or from your call\n");
- prinout(NOFLUSH);
- close(fd);
- i++;
- continue;
- }
- }
-
- if(mhdr.atbbs[0] != '\0')
- sprintf(temp,"@");
- else
- temp[0] = '\0';
- sprintf(prinbuf,
- "Message %-5.5s\nReceived from AX25, %sz/%s\nFrom: %s\nTo: %s%s%s\nSubject: %s\nStatus: %c%c\nBulletinID:%s\n\n",
- command.fld[i],
- mhdr.tim,
- mhdr.dat,
- mhdr.fromcall,
- mhdr.tocall,
- temp,
- mhdr.atbbs,
- mhdr.title,
- mhdr.type,
- mhdr.stat,
- mhdr.bid);
- prinout(NOFLUSH);
-
- if(local)
- linectr = 8;
- cp=prinbuf;
- bufcnt = 0;
- while(c=xogetc(fd)){ /*read in the message*/
- if(c != '\n'){
- *cp++ = c;
- bufcnt++;
- if(bufcnt > 254){ /*insert a newline if the user doesn't*/
- *cp++ = '\n';
- *cp = '\0';
- prinout(NOFLUSH);
- bufcnt = 0;
- cp = prinbuf;
- }
- }
- else{
- *cp++ = c;
- *cp = '\0';
- prinout(NOFLUSH);
- bufcnt = 0;
- if(local){
- linectr++;
- if(linectr > 21){
- sprintf(prinbuf,"<Hit CR to Continue>\n");
- prinout(FLUSH);
- linectr = 0;
- getline(0);
- }
- }
- cp = prinbuf;
- }
- }/*while*/
-
- /* if this is being read by the addressee, mark it as such*/
-
- if(!(strcmp(mhdr.tocall,user.call))){
- lseek(fd,0L,0); /*lock the*/
- locking(fd,1,0L); /* entire file*/
- lseek(fd,7L,0) ; /*point to 'stat ' byte in msg file*/
- if(write(fd,"Y",1) < 0) /*set it to 'read'*/
- perror("Write to message header");
- lseek(fd,0L,0); /* unlock file*/
- locking(fd,0,0L);
-
- if((fd2=openhdrfil()) < 0){
- sprintf(prinbuf,"readmail: Can't open header file\n");
- perror(prinbuf);
- }
- while(read(fd2,hdr,92)){
- fptr += 92;
- if(!strncmp(hdr,command.fld[i],strlen(command.fld[i]))){
- fptr -= 79; /*subtract 76 bytes*/
- lseek(fd,0L,0); /*lock the*/
- locking(fd,1,0L); /* entire file*/
- lseek(fd2,(long)fptr,0); /*back to 'stat' byte in hdr*/
- write(fd2,"Y",1); /* write 'Y' to it*/
- lseek(fd,0L,0); /*lock the*/
- locking(fd,1,1L); /* entire file*/
- break; /* leave loop */
- }/*if !strcmp user.call*/
- }/*while read(fd2)*/
- close(fd2);
- }/*if*/
- close(fd);
- i++; /* increment field*/
- }/* while command.fld[i] */
- }
-
- mailcheck()
- {
-
- static char hdr[128];
- int fd,fd2,hdrdone;
-
- if((fd2=openhdrfil()) < 0){
- sprintf(prinbuf,"Mailcheck: Can't open Header file\n");
- perror(prinbuf);
- return;
- }
- msgcnt=0;
-
- hdrdone=0;
-
- while(read(fd2,hdr,92)){
-
- msgcnt++; /*increment msg count*/
-
- if((fldmatch(user.call,&hdr[15]))&& (hdr[13] != 'X')){
- if(!hdrdone){
- sprintf(prinbuf,"You Have Mail");
- prinout(NOFLUSH);
- phdr();
- hdrdone=1;
- }
- sprintf(prinbuf,"%74.74s\n",hdr);
- prinout(NOFLUSH);
- }
- } /*while*/
- sprintf(prinbuf,"\n");
- prinout(FLUSH);
-
- close(fd2);
-
- if((fd=open(paramfil,O_WRONLY|O_CREAT,0x1b6)) < 0){
- sprintf(prinbuf,"mailchk:Cant open %s file\n",paramfil);
- perror(prinbuf);
- }
- else{
- sprintf(prinbuf,"%6d",msgcnt);
- lseek(fd,6l,0);
- write(fd,prinbuf,6);
- close(fd);
- }
-
- }
-
- killmsg()
- {
- char cmdstring[30];
- int i,fd,fd2,msgnum;
- char hdr[128];
- struct msghdr mhdr;
- long fptr;
-
- i=1;
- while(command.fld[i][0] != '\0' && i < 10){ /* toggle thru fields*/
- msgnum = atoi(command.fld[i]);
- sprintf(prinbuf,"%s%d",maildir,msgnum); /*check for file*/
- if((fd=open(prinbuf,O_RDONLY)) < 1){ /* skip if none*/
- sprintf(prinbuf,"%s Not Found\n",command.fld[i]);
- prinout(NOFLUSH);
- i++;
- continue;
- }
- read(fd,hdr,86);
- conhdr(hdr,&mhdr); /*get header*/
- close(fd);
- /*if from or to user or user is sysop*/
- if(((!strcmp(mhdr.tocall,user.call))
- || (command.opt=='T' && mhdr.type=='T'))
- || ((!strcmp(mhdr.fromcall,user.call))
- || (user.typ == 'S'))){
- sprintf(cmdstring,"rm %s%d",maildir,msgnum);
- system(cmdstring);
- sprintf(prinbuf,"%s killed\n",command.fld[i]);
- prinout(NOFLUSH);
- msgcnt--;
-
- if((fd2=openhdrfil()) < 0){
- sprintf(prinbuf,"readmail: Can't open header file\n");
- perror(prinbuf);
- }
-
- fptr = 0;
- while(read(fd2,hdr,92)){
- fptr += 92;
- if(!strncmp(hdr,command.fld[i],strlen(command.fld[i]))){
- fptr -= 79;
- lseek(fd2,0L,0); /* lock file*/
- locking(fd2,1,0L);
- lseek(fd2,(long)fptr,0); /*back to 'stat' byte in hdr*/
- write(fd2,"X",1); /* write 'X' to it*/
- lseek(fd2,0L,0); /* unlock file*/
- locking(fd2,0,0L);
- break; /* leave loop */
- }
- }
- close(fd2);
- }
- else{
- sprintf(prinbuf,"%s not from or to you\n",command.fld[i]);
- prinout(NOFLUSH);
- }
- i++;
- }
- }
- chkbid(bid)
- char *bid;
- {
-
- static char hdr[90];
- int fd2;
-
- if((fd2=openhdrfil()) < 0){
- sprintf(prinbuf,"chkbid: Can't open Header file\n");
- perror(prinbuf);
- return(0);
- }
-
- while(read(fd2,hdr,92)){
-
- if(indexn(bid,&hdr[73],12) >= 0){
- close(fd2);
- return(0);
- }
- }
- close(fd2);
- return(1);
- }
-
- killmine()
- {
- static char hdr[128];
- int fd2,i;
- char *cp,*bp;
-
- if((fd2=openhdrfil()) < 0){
- perror("killmine: hdr file");
- return;
- }
- i = 1;
- while(read(fd2,hdr,92) && (i < 10)){
- if(fldmatch(user.call,&hdr[15])){
- cp = command.fld[i];
- bp = hdr+15;
- while((*cp++ = *bp++) != ' ');
- *cp++ = '\0';
- i++;
- }
-
- } /*while*/
- close(fd2);
- killmsg();
- }
-
- openhdrfil() /*try to open header file, if not, sleep 5 and try again*/
- {
- int fd;
-
- if((fd = open(hdrfile,O_RDWR,0666)) < 0){
- sleep(5);
- if((fd = open(hdrfile,O_RDWR,0666)) < 0)
- return -1;
- }
- return fd;
- }
-
-